package routers

import (
	"encoding/json"
	"fmt"
	"gin-demo01/utils"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"log"
	"os"
	"path"
)

func FileRouterInit(router *gin.Engine) {
	r := router.Group("/file")
	{
		r.GET("/file-list", func(c *gin.Context) {
			pathStr := c.DefaultQuery("path", "/")
			rootPath := utils.GetCurrentAbPath()
			allPath := path.Join(rootPath, "../static", pathStr)
			os.MkdirAll(allPath, 0666)
			files, err := ioutil.ReadDir(allPath)
			if err != nil {
				log.Fatal(err)
			}
			for _, file := range files {
				fmt.Println("------------------------------------")
				fmt.Println(file.Size())
				fmt.Println(file.Name())
				fmt.Println(file.IsDir())
				jsonStu, _ := json.Marshal(file)
				fmt.Println(string(jsonStu))
			}
		})
	}
}
